package com.taiji.dicom4cloud.smart.handler;

import com.netsdk.demo.module.LoginModule;
import com.netsdk.lib.NetSDKLib;
import com.netsdk.lib.ToolKits;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class AlarmReceiveCB {



    @Autowired
    private DisConnectCB disConnectCB;

    @Autowired
    private ReConnectCB reConnectCB;

    @Autowired
    private AlaramMessageCB alaramMessageCB;

    @Autowired
    private CaptureReceiveCB captureReceiveCB;

    public void linkCamera(String pIp,String deviceId){
        //防止重复登录
        if(!CameraHelp.getCamera(pIp)){
            LoginModule.init(disConnectCB, reConnectCB);

            NetSDKLib.LLong m_hLoginHandle = LoginModule.
                    login2(pIp,37777,"admin","admin123");

            if(m_hLoginHandle.longValue() != 0 ) {
                //登录成功
                CameraHelp.addCamera(pIp);
                alaramMessageCB.setDeviceId(deviceId,m_hLoginHandle);
                //抓图订阅
                LoginModule.netsdk.CLIENT_SetSnapRevCallBack(captureReceiveCB, null);
                //报警订阅
                LoginModule.netsdk.CLIENT_SetDVRMessCallBack(alaramMessageCB, null);
                if (!LoginModule.netsdk.CLIENT_StartListenEx(LoginModule.m_hLoginHandle)) {
                    log.error("CLIENT_StartListenEx Failed!" + ToolKits.getErrorCodePrint());
                } else {
                    log.info("CLIENT_StartListenEx success.");
                }

            }  else {
                //登录失败
                log.info("登录失败:"+ ToolKits.getErrorCodePrint());
            }
        }
    }
}